package com.esunny.resourcevisualization.manage;

import com.esunny.resourcevisualization.dao.ResourceTypeEntityDataDao;
import com.esunny.resourcevisualization.dao.ResourceTypeFieldDao;
import com.esunny.resourcevisualization.domain.entity.ResourceTypeField;
import com.esunny.resourcevisualization.domain.response.ReturnStructure;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version 1.0.0.0
 * @InterfaceName ResourceTypeFieldEntityDataManage
 * @Description 资源类别字段-资源类别实体字段值联合数据处理
 * @Author baochencong
 * @Date 2020/9/28 13:10
 */
@Component
public class ResourceTypeFieldEntityDataManage {

    @Resource
    private ResourceTypeFieldDao resourceTypeFieldDao;

    @Resource
    private ResourceTypeEntityDataDao resourceTypeEntityDataDao;

    private static Logger LOGGER = LoggerFactory.getLogger(ResourceTypeFieldEntityDataManage.class);

    /**
     * 通过资源类别字段id删除相关资源类别字段信息
     * @param fieldId 资源类别字段id
     * @return 删除结果信息
     */
    public ReturnStructure deleteResourceTypeFieldInfoByFieldId(int fieldId){
        ResourceTypeField resourceTypeFieldId = resourceTypeFieldDao.getResourceTypeFieldId(fieldId);
        String name = resourceTypeFieldId.getResourceTypeName();
        String fieldName = resourceTypeFieldId.getResourceTypeFieldName();
        try{
            resourceTypeEntityDataDao.deleteResourceTypeEntityDataByNameAndFieldName(name,fieldName);
            resourceTypeFieldDao.deleteResourceTypeFieldByFieldId(fieldId);
        }catch (Exception e){
            LOGGER.error("删除资源类别字段失败，错误信息为");
            LOGGER.error(String.valueOf(e));
            return ReturnStructure.ERROR("删除资源类别字段失败",null);
        }
        return ReturnStructure.SUCCESS("删除资源类别字段成功",null);
    }

    /**
     * 通过资源类别字段id修改相关资源类别字段信息
     * @param oldFieldName 原来的资源类别字段英文名
     * @param resourceTypeField 资源类别字段信息
     * @return 修改结果信息
     */
    public ReturnStructure updateResourceTypeFieldInfo(String oldFieldName, ResourceTypeField resourceTypeField){
        String newFieldName = resourceTypeField.getResourceTypeFieldName();
        try{
            resourceTypeEntityDataDao.updateResourceTypeEntityDataByFieldName(oldFieldName,newFieldName);
            resourceTypeFieldDao.updateResourceTyprFieldByFieldId(resourceTypeField);
        }catch (Exception e){
            LOGGER.error("修改资源类别字段信息失败，错误信息为");
            LOGGER.error(String.valueOf(e));
            return ReturnStructure.ERROR("修改资源类别字段信息失败",null);
        }
        return ReturnStructure.SUCCESS("修改资源类别字段信息成功",null);
    }

}
